﻿using ConsoleApp2.Enum;
using ConsoleApp2.Models;
using ConsoleApp2.Service;

namespace ConsoleApp2.Impl;

public class PermissionServiceImpl : PermissionService
{
    public List<Permission> AllPermissions { get; set; }

    public PermissionServiceImpl()
    {
        AllPermissions = LocalPermission.GetLocalPermissions();
    }

    public List<Permission> GetAllPermissions()
    {
        if (AllPermissions == null)
        {
            AllPermissions = LocalPermission.GetLocalPermissions();
        }
        return AllPermissions;
    }

    public bool UserHavePermission(ConsoleApp2.Models.User user, PermissionName queryUser)
    {
        List<Role> roles = user.Roles;
        foreach (var role in roles)
        {
            List<Permission> permissions = role.Permissions;
            foreach (var permission in permissions)
            {
                if (permission.PermissionName == queryUser)
                {
                    return true;
                }
            }
        }
        return false;
    }
}